package server;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class PlayerThread implements Runnable {

	private Thread currentThread;

	private BufferedReader in;
	private DataOutputStream out;
	private String playerName, playerColor;
	private GameData game;

	private Queue<String> events;

	public PlayerThread(String name, String color, Socket socket, BufferedReader in, DataOutputStream out) {
		this.playerName = name;
		this.in = in;
		this.setOut(out);
		this.playerColor = color;
		game = GameData.getUnqiueInstance();
		events = new ArrayBlockingQueue<String>(200);
	}

	public void run() {
		currentThread = Thread.currentThread();
		try {

			boolean connected = true;

			if (!connected) {
				throw new Error("Could not connect to player properly.");
			}

			while (connected) {

				String buffer = in.readLine();

				String[] e = buffer.split("_");

				for (String s : e) {
					String[] information = s.split("#");
//					System.out.println(s);
					if (information[0].equals("4")) { // 4 = disconnect
						connected = false;
						System.out.println("LORLSORS");
					} else if (information[0].equals("3")) { // 3 = player move
						if(!game.canEnter(this)) {
							try {
								this.wait();
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}
						}
						if (information[1].equals("move")) {
							String direction = information[2];
							game.calculateMove(this, intify(direction));
							
						} else if (information[1].equals("shoot")) {
							game.calculateShot(this);
						}
						game.releaseCritical();
					}
				}

				String event = "";
				String temp;
				while ((temp = events.poll()) != null) {
					event += temp + "_";
				}

				if (event.equals(""))
					event = "3#ping";

				event += "\n";

				if (connected) {
					out.writeBytes(event);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
			System.out.println("Player " + playerName + " disconnected!");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * omdanner en string direction til en int direction
	 * 
	 * @param direction
	 * @return
	 */
	private int intify(String direction) {
		// facing(0=up,1=right,2=down,3=left)
		int intified = -1;
		if (direction.equals("up")) {
			intified = 0;
		} else if (direction.equals("left")) {
			intified = 3;
		} else if (direction.equals("right")) {
			intified = 1;
		} else if (direction.equals("down")) {
			intified = 2;
		}
		return intified;
	}

	public boolean addEvent(String s) {
		boolean temp = false;
		synchronized(this) {
			temp = events.offer(s);
		}
		return temp;
	}

	public DataOutputStream getOut() {
		return out;
	}

	public void setOut(DataOutputStream out) {
		this.out = out;
	}

	public String getPlayerName() {
		return playerName;
	}
	
	public String getPlayerColor() {
		return playerColor;
	}

	public void interrupt() {
		currentThread.interrupt();
	}
	
	public String toString() {
		return playerName;
	}
}